local sidong = fk.CreateSkill {
  name = "th_sidong",
}

sidong:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  x = #room.logic:getEventsByRule(GameEvent.UseCard, 3, function (e)
    return e.data.from == player
  end, nil, Player.HistoryTurn)
  room:setPlayerMark(player, "th_sidong_times-turn", x)
end)

sidong:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(sidong.name) and #player:getPile("$th_mamizou_ye") ~= 0
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToUseRealCard(player, {
      pattern = ".|.|.|$th_mamizou_ye",
      expand_pile = "$th_mamizou_ye",
      skill_name = sidong.name,
      prompt = "#th_sidong-invoke0",
      skip = true,
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useCard(event:getCostData(self))
    while #player:getPile("$th_mamizou_ye") ~= 0 and player.room:askToUseRealCard(player, {
      pattern = ".|.|.|$th_mamizou_ye",
      expand_pile = "$th_mamizou_ye",
      skill_name = sidong.name,
      prompt = "#th_sidong-invoke",
    }) do
      --do nothing
    end
  end
})
sidong:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sidong.name) then
      return player:getMark("th_sidong_times-turn") == 2
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = sidong.name,
      prompt = "#th_sidong-draw"
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, sidong.name)
    if player.dead or player:isKongcheng() then return end
    local card = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = sidong.name,
      prompt = "#th_sidong-put",
      cancelable = false,
    })
    if #card > 0 then
      room:moveCards{
        ids = card,
        from = player,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = sidong.name,
        drawPilePosition = -1,
        moveVisible = false,
      }
    end
  end,

  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "th_sidong_times-turn")
  end
})

Fk:loadTranslationTable{
  ["th_sidong"] = "伺动",
  [":th_sidong"] = "每回合开始时，你可以依次使用任意张“叶”；你每回合使用第二张牌时，你可以摸两张牌并将一张手牌置于牌堆底。",

  ["#th_sidong-invoke0"] = "伺动：你可以依次使用任意张“叶”",
  ["#th_sidong-invoke"] = "伺动：你可以使用一张“叶”，取消则终止此流程",
  ["#th_sidong-draw"] = "伺动：你可以摸两张牌并将一张手牌置于牌堆底",
  ["#th_sidong-put"] = "伺动：请将一张手牌置于牌堆底",
}

return sidong
